package top.shaozuo.simple.configuration.microprofile.config;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;

import top.shaozuo.simple.configuration.microprofile.config.converts.Converters;
import top.shaozuo.simple.configuration.microprofile.config.source.ConfigSources;

public class DefaultConfigBuilder implements ConfigBuilder {

	private ClassLoader classLoader;
	private final ConfigSources configSources;
	private final Converters converters;

	public DefaultConfigBuilder(ClassLoader classLoader) {
		this.classLoader = classLoader;
		configSources = new ConfigSources(classLoader);
		converters = new Converters(classLoader);
	}

	@Override
	public ConfigBuilder addDefaultSources() {
		configSources.addDefaultSources();
		return this;
	}

	@Override
	public ConfigBuilder addDiscoveredSources() {
		configSources.addDiscoveredSources();
		return this;
	}

	@Override
	public ConfigBuilder addDiscoveredConverters() {
		converters.addDiscoveredConverters();
		return this;
	}

	@Override
	public ConfigBuilder forClassLoader(ClassLoader loader) {
		configSources.setClassLoader(loader);
		converters.setClassLoader(loader);
		return this;
	}

	@Override
	public ConfigBuilder withSources(ConfigSource... sources) {
		configSources.addConfigSources(sources);
		return this;
	}

	@Override
	public ConfigBuilder withConverters(Converter<?>... converters) {
		this.converters.addConverters(converters);
		return this;
	}

	@Override
	public <T> ConfigBuilder withConverter(Class<T> type, int priority, Converter<T> converter) {
		converters.addConverter(type, priority, converter);
		return this;
	}

	@Override
	public Config build() {
		Config config = new JavaConfig(configSources, converters);
		return config;
	}

}
